package com.yanqu.road.server.cmd.activity.shuihu;

import com.yanqu.road.entity.activity.shuihu.ShuiHuUserData;
import com.yanqu.road.pb.activity.ShuiHuActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihu.ShuiHuModule;
import com.yanqu.road.server.pb.ShuiHuPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SHUIHU_ACTIVITY_DRAW_CARD_FROM_CROSS, desc = "跨服返回抽卡结果")
public class ShuiHuDrawCardFromCrossCmd extends AbstractCommand {

    private int cardId = -1;

    private ShuiHuUserData userData = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuActivityProto.CrossDrawCardRespMsg reqMsg = ShuiHuActivityProto.CrossDrawCardRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if (ret == 0) {
            cardId = reqMsg.getCardId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuModule shuiHuModule = player.getModule(ShuiHuModule.class);
        shuiHuModule.drawCardFromCross(ret, cardId);

        userData = shuiHuModule.getUserData();
    }

    @Override
    public void sendData(GamePlayer player) {
        ShuiHuActivityProto.DrawCardRespMsg.Builder builder = ShuiHuActivityProto.DrawCardRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setCardId(cardId);
        }
        if (userData != null) {
            builder.setUserData(ShuiHuPb.parseShuiHuUserDataMsg(userData));
        }
        player.sendPacket(ClientProtocol.U_SHUIHU_ACTIVITY_DRAW_CARD, builder);
    }
}
